package cn.edu.ldu.modules.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.edu.ldu.http.rqtUtils;
import cn.edu.ldu.modules.entity.FriendsEntity;
import cn.edu.ldu.util.Consts;

import java.util.List;

public class FriendsController extends BaseController {

    public static List<FriendsEntity> getFriendsByUserId(Long userId, String token) throws Exception {

        JSONObject jsonObject = rqtUtils.httpclientGet(new StringBuffer(Consts.getFriendsByIUserId).append("/").append(userId), token);
        if(jsonObject.getString("msg").equals("success")){
            return JSONObject.parseArray(jsonObject.getString("friends"), FriendsEntity.class);
        }
        return null;
    }
    public static FriendsEntity getFriendByUserId(Long userId, Long friendId, String token) throws Exception {

        JSONObject jsonObject = rqtUtils.httpclientGet(new StringBuffer(Consts.getFriendByIUserId).append("/").append(userId).append("?friendId=").append(friendId), token);
        if(jsonObject.getString("msg").equals("success")){
            return JSONObject.parseObject(jsonObject.getString("friends"), FriendsEntity.class);
        }
        return null;
    }


    public static List<FriendsEntity> getWithoutHandleMsg(Long userId, Integer status, String token) throws Exception {
        JSONObject jsonObject = rqtUtils.httpclientGet(new StringBuffer(Consts.getWithoutHandleMSG_FRIEND).append("/").append(userId).append("?status=").append(status), token);


        if(jsonObject.getString("msg").equals("success")){
            return JSONObject.parseArray(jsonObject.getString("friends"), FriendsEntity.class);
        }
        return null;
    }

    public static void addFriend(FriendsEntity friendsEntity, String token) throws Exception {

        String jsonString = JSONObject.toJSONString(friendsEntity);

        String responseResult = rqtUtils.httpclientPost(new StringBuffer(Consts.addFriend), jsonString, token);

    }

    public static String updateFriend(FriendsEntity friendsEntity, String token) throws Exception {

        String jsonString = JSONObject.toJSONString(friendsEntity);

        String responseResult = rqtUtils.httpclientPost(new StringBuffer(Consts.updateFriend), jsonString, token);

        return JSONObject.parseObject(responseResult).getString("msg");
    }

    public static String deleteFriend(FriendsEntity friendsEntity, String token) throws Exception {

        String jsonString = JSONObject.toJSONString(friendsEntity);

        String responseResult = rqtUtils.httpclientPost(new StringBuffer(Consts.deleteFriend), jsonString, token);

        return JSONObject.parseObject(responseResult).getString("msg");
    }

}
